package com.paireach.bw.adapter;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.paireach.bw.R;
import com.paireach.bw.activity.DriverExceptionPostActivity;
import com.paireach.bw.activity.MainActivity;
import com.paireach.bw.bean.DriveOrder;
import com.paireach.bw.utils.LogUtil;

import java.util.List;

/**
 * Created by ${cqc} on 2017/2/16.
 */

public class Frag2Adapter extends RecyclerView.Adapter {

    private List<DriveOrder> list;
    private AlertDialog dialog;
    private MyViewHolder holder;

    public Frag2Adapter(List<DriveOrder> list) {
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_frag1, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
        holder = (MyViewHolder) viewHolder;

        final DriveOrder info = list.get(position);

        holder.tv1.setBackgroundResource(R.drawable.tv_frag1_frag2_shape);
        holder.tv1.setText("配送中");
        holder.checkBox.setVisibility(View.GONE);
        holder.tv_order_num.setText("负载单号：" + info.getCODE());
        holder.tv_origin_name.setText("发货地名称：" + info.getSOURCE_NAME());
        holder.tv_origin_address.setText("发货地址：" + info.getSOURCE_ADDR());
        holder.tv_origin_time.setText("预约发货时间：" + info.getPLAN_DELIVER_TIME());

        String transport_code = info.getTRANSPORT_CODE();
        if ("KA".equals(transport_code)) {//KA
            holder.tv_order_style.setText("KA托盘");
        } else if ("BACK".equals(transport_code)) {//回瓶
            holder.tv_order_style.setText("回空");
        }else if ("COMMON".equals(transport_code)) {//回瓶
            holder.tv_order_style.setText("常规");
        }

        holder.tv_order_state.setText(info.getSTATUS_NAME());
        holder.tv_weight.setText("货物吨重：" + info.getTOTAL_WEIGHT() + "kg");
        holder.tv_end_name.setText("收货地名称：" + info.getDC_NAME());
        holder.tv_end_address.setText("收货地地址：" + info.getDC_ADDRESS());
        holder.tv_end_time.setText("预约到达时间：" + info.getPLAN_ACHIEVE_TIME());
        holder.iv_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // TODO: 2017/3/7  
//                showPhoneDialog(view.getContext());
            }
        });
        holder.tv_exception_commit.setVisibility(View.VISIBLE);
        holder.tv_exception_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.d("单号="+info.getCODE()+",单号2="+list.get(position).getCODE());
                MainActivity context = (MainActivity) holder.itemView.getContext();
                ActivityOptionsCompat options8 = ActivityOptionsCompat.makeSceneTransitionAnimation(context);
                Intent intent = new Intent(context, DriverExceptionPostActivity.class);
                intent.putExtra("orderCode", list.get(position).getCODE());
                ActivityCompat.startActivity(context, intent, options8.toBundle());
            }
        });

        if (onItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onItemClickListener.onItemClick(view, position);
                }
            });
        }

        AnimatorSet set = new AnimatorSet();
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(holder.itemView, "scaleX", 0.3f, 1.0f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(holder.itemView, "scaleY", 0.3f, 1.0f);
        set.setDuration(500);
        set.playTogether(scaleX, scaleY);
        set.start();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    private class MyViewHolder extends RecyclerView.ViewHolder {

        TextView checkBox;
        TextView tv_exception_commit;//异常上报
        ImageView iv_call;
        TextView tv_order_num;
        TextView tv1;
        TextView tv_origin_name;
        TextView tv_origin_address;
        TextView tv_origin_time;
        TextView tv_order_style;
        TextView tv_order_state;
        TextView tv_weight;
        TextView tv_end_name;
        TextView tv_end_address;
        TextView tv_end_time;

        public MyViewHolder(View itemView) {
            super(itemView);
            checkBox = (TextView) itemView.findViewById(R.id.checkBox);
            tv_exception_commit = (TextView)itemView.findViewById(R.id.tv_exception_commit);
            tv1 = (TextView) itemView.findViewById(R.id.tv1);
            iv_call = (ImageView) itemView.findViewById(R.id.iv_call);
            tv_order_num = (TextView) itemView.findViewById(R.id.tv_order_num);
            tv_origin_name = (TextView) itemView.findViewById(R.id.tv_origin_name);
            tv_origin_address = (TextView) itemView.findViewById(R.id.tv_origin_address);
            tv_origin_time = (TextView) itemView.findViewById(R.id.tv_origin_time);
            tv_order_style = (TextView) itemView.findViewById(R.id.tv_order_style);
            tv_order_state = (TextView) itemView.findViewById(R.id.tv_order_state);
            tv_weight = (TextView) itemView.findViewById(R.id.tv_weight);
            tv_end_name = (TextView) itemView.findViewById(R.id.tv_end_name);
            tv_end_address = (TextView) itemView.findViewById(R.id.tv_end_address);
            tv_end_time = (TextView) itemView.findViewById(R.id.tv_end_time);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    //拨打电话的dialog
    private void showPhoneDialog(final Context context) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("SCMC服务电话");
        builder.setMessage("是否拨打电话:" + context.getResources().getString(R.string.service_phone));
        builder.setCancelable(true);
        builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();

                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + context.getResources().getString(R.string.service_phone)));
                context.startActivity(intent);
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
            }
        });
        dialog = builder.create();
        dialog.show();
    }
}
